package com.hxzy.controller;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.util.RedisUtil;
import com.hxzy.common.BaseServlet;
import com.hxzy.common.ResultAjax;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:
 * @Author: ykj
 * @Date: 2022/3/7 19:26
 */
@WebServlet(urlPatterns = "/user/logout")
public class LogoutServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除redis中相关的数据
        String token = req.getHeader("UserToken");
        String loginSuccessReidsKey="login_total:"+token;
        deleteRedisKey(loginSuccessReidsKey);
        ResultAjax success = ResultAjax.success();
        super.render(resp, JSONObject.toJSONString(success));

    }

    private long deleteRedisKey(String redisKey){
        Jedis jedis= RedisUtil.create();
        return jedis.del(redisKey);
    }

}
